package com.shu.mqtt;

import java.net.URISyntaxException;

import org.fusesource.hawtbuf.Buffer;
import org.fusesource.hawtbuf.UTF8Buffer;
import org.fusesource.hawtdispatch.Dispatch;
import org.fusesource.mqtt.client.Callback;
import org.fusesource.mqtt.client.CallbackConnection;
import org.fusesource.mqtt.client.Listener;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.QoS;
import org.fusesource.mqtt.client.Topic;

public class MqttSubscribe {
	
	static String topic = "apk/heartbeat";
	
    public static void main(String[] args) throws URISyntaxException {
        MQTT mqtt = new MQTT();
        mqtt.setHost("tcp://192.168.1.15:1882");
        mqtt.setUserName("admin");
        mqtt.setPassword("public");
        mqtt.setKeepAlive((short) 5);
        mqtt.setClientId("uu000000");
//        mqtt.setDispatchQueue(Dispatch.createQueue("mqtt.test.aaa"));
//        mqtt.setDispatchQueue
        final CallbackConnection connection = mqtt.callbackConnection();
        connection.listener(new ConnListener());
        
        connection.connect(new ConnCallback());

        Topic[] topics = { new Topic(topic, QoS.AT_LEAST_ONCE) };
		connection.subscribe(topics, new Callback<byte[]>() {
            public void onSuccess(byte[] qoses) {
                System.out.println("subscribe success");
                System.out.println(new String(qoses).toString());
            }

            public void onFailure(Throwable value) {
                System.out.println("subscribe fail");
            }
        });
        
    }
}